package cn.qn.controller.news;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.news.VideoDTO;
import cn.qn.dto.news.VideoQueryDTO;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.service.news.VideoService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jeet
 * @description
 * @date 2019/5/31 17:51
 */
@RestController
@RequestMapping("/news/video")
@Slf4j
public class VideoController {

    @Reference(check = false)
    private VideoService videoService;

    @RequestMapping("/insert")
    public ResponseDTO insert(@RequestBody VideoDTO dto){
        if(null == dto.getName() || StringUtils.isBlank(dto.getName())){
            return ResponseDTO.error("新闻名称不能为空");
        }
        if( null == dto.getCover()){
            return ResponseDTO.error("新闻封面不能为空");
        }
        if( null == dto.getBanner()){
            return ResponseDTO.error("新闻Banner不能为空");
        }
        if(null == dto.getUrl()){
            return ResponseDTO.error("新闻视频不能为空");
        }

        dto.setUpdateBy(MemberAccountUtils.getCurrentMember().getUserId());
        if(null == dto.getId()){
            dto.setCreateBy(MemberAccountUtils.getCurrentMember().getUserId());
            videoService.insert(dto);
        }else{
            videoService.update(dto);
        }

        return ResponseDTO.success(true,"保存成功");
    }

    @RequestMapping("/page")
    public ResponseDTO page(@RequestBody VideoQueryDTO baseQuery){
        PageVO<VideoDTO> albumDTOPageVO = videoService.page(baseQuery);
        return ResponseDTO.success(albumDTOPageVO);
    }

    @RequestMapping("/findById/{id}")
    public ResponseDTO findById(@PathVariable Integer id){
        VideoDTO albumDTO = videoService.findById(id);
        return ResponseDTO.success(albumDTO);
    }
}
